package com.booksystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booksystem.common.RestResult;
import com.booksystem.dto.*;
import com.booksystem.dto.CategoryAddDTO;
import com.booksystem.dto.CategoryDeleteDTO;
import com.booksystem.dto.CategoryEditDTO;
import com.booksystem.dto.CategoryQueryDetailDTO;
import com.booksystem.dto.CategoryQueryListDTO;
import com.booksystem.entity.CategoryDO;
import com.booksystem.exception.BusinessException;
import com.booksystem.mapper.CategoryMapper;
import com.booksystem.service.CategoryService;
import com.booksystem.utils.ResultCodeConstant;
import java.util.Date;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 商品分类服务实现类
 * </p>
 * @author 段伟豪
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryDO> implements CategoryService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult<Boolean> addCategory(CategoryAddDTO categoryAddDTO) {
        // 校验分类名称是否已存在
        QueryWrapper<CategoryDO> wrapper = new QueryWrapper<>();
        wrapper.eq("category_name", categoryAddDTO.getCategoryName());
        CategoryDO existingCategory = this.getOne(wrapper);
        if (existingCategory != null) {
            return new RestResult<>(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG, false);
        }

        // 构建分类DO对象并保存
        CategoryDO categoryDO = new CategoryDO();
        BeanUtils.copyProperties(categoryAddDTO, categoryDO);
        categoryDO.setCreateTime(new Date());
        categoryDO.setUpdateTime(new Date());

        boolean result = this.save(categoryDO);
        if (!result) {
            throw new BusinessException(ResultCodeConstant.CODE_000002, ResultCodeConstant.CODE_000002_MSG);
        }
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult<Boolean> editCategory(CategoryEditDTO categoryEditDTO) {
        // 根据分类ID查询分类是否存在
        CategoryDO existCategory = this.getById(categoryEditDTO.getCategoryId());
        if (existCategory == null) {
            return new RestResult<>(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG, false);
        }

        // 更新分类信息
        CategoryDO categoryDO = new CategoryDO();
        BeanUtils.copyProperties(categoryEditDTO, categoryDO);
        categoryDO.setCategoryId(existCategory.getCategoryId()); // 设置主键ID
        categoryDO.setUpdateTime(new Date());

        boolean result = this.updateById(categoryDO);
        if (!result) {
            throw new BusinessException(ResultCodeConstant.CODE_000002, ResultCodeConstant.CODE_000002_MSG);
        }
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, true);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult<Boolean> deleteCategory(CategoryDeleteDTO categoryDeleteDTO) {
        // 根据分类ID查询分类是否存在
        CategoryDO existCategory = this.getById(categoryDeleteDTO.getCategoryId());
        if (existCategory == null) {
            return new RestResult<>(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG, false);
        }

        // 判断该分类下是否有子分类或商品关联
        QueryWrapper<CategoryDO> childQuery = new QueryWrapper<>();
        childQuery.eq("parent_id", categoryDeleteDTO.getCategoryId());
        long countChild = this.count(childQuery);
        if (countChild > 0) {
            return new RestResult<>(ResultCodeConstant.CODE_000001, "分类下存在子分类，无法删除", false);
        }

        // 执行删除操作
        boolean result = this.removeById(categoryDeleteDTO.getCategoryId());
        if (!result) {
            throw new BusinessException(ResultCodeConstant.CODE_000002, ResultCodeConstant.CODE_000002_MSG);
        }
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, true);
    }


    @Override
    public RestResult<List<CategoryDO>> listCategories(CategoryQueryListDTO categoryQueryListDTO) {
        QueryWrapper<CategoryDO> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", categoryQueryListDTO.getParentId())
                .eq("is_active", categoryQueryListDTO.getIsActive());

        List<CategoryDO> categories = this.list(wrapper);
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, categories);
    }

    @Override
    public RestResult<CategoryDO> getCategoryDetail(CategoryQueryDetailDTO categoryQueryDetailDTO) {
        CategoryDO categoryDO = this.getById(categoryQueryDetailDTO.getCategoryId());
        if (categoryDO == null) {
            return new RestResult<>(ResultCodeConstant.CODE_000001, ResultCodeConstant.CODE_000001_MSG, null);
        }
        return new RestResult<>(ResultCodeConstant.CODE_000000, ResultCodeConstant.CODE_000000_MSG, categoryDO);
    }
}